package com.example.tipcalculator;

import com.example.tipcalculator.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    //Calculate the numbers
    public void calculate(View view)
    {
    	NumberFormat formatter = NumberFormat.getCurrencyInstance();
    	
    	EditText amountBillText = (EditText) findViewById(R.id.amount);
    	EditText percentageText = (EditText) findViewById(R.id.percentage);
    	EditText numPeopleText = (EditText) findViewById(R.id.numOfPeople);
    	
    	if(amountBillText.getText().toString().equals("") || amountBillText.getText().toString().equals(" ") 
    			|| percentageText.getText().toString().equals("") || percentageText.getText().toString().equals(" ") 
    			|| numPeopleText.getText().toString().equals("") || numPeopleText.getText().toString().equals(" ") )
    	{
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setTitle("Error");
    		builder.setMessage("Please fill out all three fields. If unsure, enter 0.");
    		builder.setNeutralButton("OK", new DialogInterface.OnClickListener() 
    		{
    			public void onClick(DialogInterface dialog, int which) 
    			{
            	
    			}
    		});
    		
    		AlertDialog alert = builder.create();
    		alert.show();
    	}
    	else
    	{
    	double amount = Double.parseDouble(amountBillText.getText().toString());
    	double percentage = Double.parseDouble(percentageText.getText().toString());
    	int numPeople = Integer.parseInt(numPeopleText.getText().toString());
    	
    	double tip, total, totalPerPerson;
    	
    	total = amount;
    	
    	tip = (percentage * amount) / 100;
    	
    	total += tip;
    	
    	totalPerPerson = total / numPeople;
    	
    	TextView textTip = (TextView) findViewById(R.id.text_tip);
    	TextView textTotal = (TextView) findViewById(R.id.text_total);
    	TextView textPerPerson = (TextView) findViewById(R.id.text_per_person);
    	
    	textTip.setText(formatter.format(tip));
    	textTotal.setText(formatter.format(total));
    	textPerPerson.setText(formatter.format(totalPerPerson));
    	}
    }
    
    //Reset all the fields
    public void reset(View view)
    {
    	EditText amountBillText = (EditText) findViewById(R.id.amount);
    	EditText percentageText = (EditText) findViewById(R.id.percentage);
    	EditText numPeopleText = (EditText) findViewById(R.id.numOfPeople);
    	
    	amountBillText.setText("");
    	percentageText.setText("");
    	numPeopleText.setText("");
    	
    	TextView textTip = (TextView) findViewById(R.id.text_tip);
    	TextView textTotal = (TextView) findViewById(R.id.text_total);
    	TextView textPerPerson = (TextView) findViewById(R.id.text_per_person);
    	
    	textTip.setText("");
    	textTotal.setText("");
    	textPerPerson.setText("");
    	
    }
}
